<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\admin\controller;

class Role extends \app\Rest
{
    public function __construct(\think\App $app)
    {
        parent::__construct($app);
    }
    public function createRole()
    {
        $role = $this->_input["role"];
        $role["role_id"] = uuid();
        $role["uniacid"] = $this->_uniacid;
        $role_model = new \app\admin\model\Role();
        $result = $role_model->createRole($role);
        return $this->seccess($result);
    }
    public function listRole()
    {
        $param = $this->_param;
        $page_config = ["page" => 1, "page_count" => 20];
        if(isset($param["page"]) && 0 < $param["page"]) {
            $page_config["page"] = $param["page"];
        }
        if(isset($param["page_count"]) && 0 < $param["page_count"]) {
            $page_config["page_count"] = $param["page_count"];
        }
        $filter = $param;
        $filter["uniacid"] = $this->_uniacid;
        $role_model = new \app\admin\model\Role();
        $page_config["totle"] = $role_model->listRoleCount($filter);
        $roles = $role_model->listRole($filter, $page_config);
        $page_config["total_page"] = (int) ($page_config["total"] / $page_config["page_count"]);
        if(0 < $page_config["total"] % $page_config["page_count"]) {
            $page_config["total_page"] = $page_config["total_page"] + 1;
        }
        $result = $page_config;
        $result["roles"] = $roles;
        return $this->success($result);
    }
    public function getRole()
    {
        $role_id = $this->_param["role_id"];
        $role_model = new \app\admin\model\Role();
        $role = $role_model->getRole(["role_id" => $role_id, "uniacid" => $this->_uniacid]);
        return $this->success($role);
    }
    public function updateRole()
    {
        $role_id = $this->_param["role_id"];
        $role_model = new \app\admin\model\Role();
        $role = $role_model->getRole(["role_id" => $role_id, "uniacid" => $this->_uniacid]);
        if(empty($role)) {
            return $this->error("the role is nit exist ,please check the role id.");
        }
        $role = $this->_input["role"];
        $result = $role_model->updateRole(["role_id" => $role_id, "uniacid" => $this->_uniacid], $role);
        return $this->seccess($result);
    }
    public function delRole()
    {
        $role_id = $this->_param["role_id"];
        $role_model = new \app\admin\model\Role();
        $role = $role_model->getRole(["role_id" => $role_id, "uniacid" => $this->_uniacid]);
        if(empty($role)) {
            return $this->error("the role is nit exist ,please check the role id.");
        }
        $result = $role_model->delRole(["role_id" => $role_id, "uniacid" => $this->_uniacid]);
        if(!empty($result)) {
            $user_model = new \app\admin\model\User();
            $user_model->update(["role_id" => $role_id, "uniacid" => $this->_uniacid], ["role_id" => 0]);
        }
        return $this->seccess($result);
    }
}

?>